/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import javax.swing.JOptionPane;
import models.dados.JdbcDAO;

/**
 *
 * @author Wenderson
 */
public class HibernateConfig {
   private String user = null;
   private String password = null;
   private String url = null;
   private String database = null;
   
   private void setPropriedades() {
       Properties properties = new Properties();
       
       properties.setProperty("jdbc.user", "frame");
       properties.setProperty("jdbc.password", "framework");
       properties.setProperty("jdbc.url", "jdbc:sqlserver://localhost:1433;databaseName");
       properties.setProperty("jdbc.database", "FrameWork");
       
       try {
           File file = new File("configFrameWork.xml");
           
           if (!file.exists()) {
               file.createNewFile();
           }
           
           FileOutputStream fos = new FileOutputStream("configFrameWork.xml");
           properties.storeToXML(fos, "FILE JDBC PROPERTIES:", "ISO-8859-1");
           fos.close();
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
   }
   
   public Properties getProperties() {
       this.atualizaPropriedades();
       
       if (!this.propriedadesValidas()) {
           this.setPropriedades();
       }
       
       Properties p = new Properties();
       p.setProperty("hibernate.connection.url", this.getUrl() + "=" + this.getDatabase()
            + ";IntegratedSecurity=true");
       p.setProperty("hibernate.connection.username", this.getUser());
       p.setProperty("hibernate.connection.password", this.getPassword());
       
       if (!this.conexaoValida(this.getUser(), this.getPassword(), this.getUrl() + "=" + this.getDatabase()
                + ";IntegratedSecurity=true")) {
//           p = new Properties();
//           p.setProperty("hibernate.connection.url", this.getUrl() + "=" + this.getDatabase()
//                 + ";IntegratedSecurity=true");
//           
//           if (!this.conexaoValida(url))
           return null;
       }
       
       return p;
   }
   
   private boolean propriedadesValidas() {
       if (((this.getDatabase() != null) && !this.getDatabase().trim().isEmpty()) && 
           ((this.getPassword() != null) && !this.getPassword().trim().isEmpty()) && 
           ((this.getUrl() != null) && !this.getUrl().trim().isEmpty()) && 
           ((this.getUser() != null) && !this.getUser().trim().isEmpty())) {
           return true;
       }
       
       return false;
   }
   
   public void atualizaPropriedades() {
       Properties properties = new Properties();
       
       try {
           File file = new File("configFrameWork.xml");
           
           if (!file.exists()) {
               this.setPropriedades();
           }
           
           FileInputStream fis = new FileInputStream("configFrameWork.xml");
           properties.loadFromXML(fis);
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
       
       this.setUser(properties.getProperty("jdbc.user"));
       this.setPassword(properties.getProperty("jdbc.password"));
       this.setUrl(properties.getProperty("jdbc.url"));
       this.setDatabase(properties.getProperty("jdbc.database"));
   }
   
   public boolean conexaoValida(String user, String password, String url) {
       try {
           Connection conn = 
                   DriverManager.getConnection(url, user, password);
           
           if (conn != null) {
               conn.close();
               return true;
           }
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
       
       return false;
   }
   
   public boolean conexaoValida(String url) {
       try {
           Connection conn = 
                   DriverManager.getConnection(url, user, password);
           
           if (conn != null) {
               conn.close();
               return true;
           }
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
       
       return false;
   }
   
   public static void main(String[] args) {
       if (new JdbcConnection().getConnection() != null) {
           System.out.println("Conectado");
       } else {
           System.out.println("Não Conectado");
       }
       
       controllers.ProcedimentoController.obterTodos();
       
       int id = new JdbcDAO().nextId("BD_CONCESSIONARIA.SCH_DW", "DIM_MONTADORA");
       System.out.println(id);
   }

   //<editor-fold defaultstate="collapsed" desc="Gets e Sets">
   public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
    //</editor-fold>
}
